﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace USBInterface
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    public class CurvePoint
    {
        public CurvePoint(float x = 0, float y = 0)
        {
            this.x = x;
            this.y = y;
        }
        public float x { get; set; }
        public float y { get; set; }
    }

    // 之所以要再定义一个结构体是因为在通信协议里面用类的数组在求大小的时候会有问题
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    public struct CurvePoint_T
    {
        public CurvePoint_T(float x = 0, float y = 0)
        {
            this.x = x;
            this.y = y;
        }
        public float x { get; set; }
        public float y { get; set; }
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    public class WEVReply_T
    {
        public byte ret_code { get; set; }
        public byte count { get; set; }
    }

    public enum CurveId_T
    {
        CURVE_PID_VEL = 1,
        CURVE_TORQUE_POS
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 1)]
    public class WevUserDefinedCurve_T
    {
        public WevUserDefinedCurve_T(byte id)
        {
            curve_id = id;
            count = 0;
            point_counts = 1;
            points = new CurvePoint_T[20];
            for (int i = 0; i < points.Length; i++) points[i] = new CurvePoint_T();
        }
        public byte curve_id { get; set; }
        public byte count { get; set; }
        public byte point_counts { get; set; }

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public CurvePoint_T[] points;
    }
}

namespace MotorTypeDef
{
    public enum Error
    {
        ERROR_NONE = 0x00000000,
        ERROR_PHASE_RESISTANCE_OUT_OF_RANGE = 0x00000001,
        ERROR_PHASE_INDUCTANCE_OUT_OF_RANGE = 0x00000002,
        ERROR_ADC_FAILED = 0x00000004,
        ERROR_DRV_FAULT = 0x00000008,
        ERROR_CONTROL_DEADLINE_MISSED = 0x00000010,
        ERROR_NOT_IMPLEMENTED_MOTOR_TYPE = 0x00000020,
        ERROR_BRAKE_CURRENT_OUT_OF_RANGE = 0x00000040,
        ERROR_MODULATION_MAGNITUDE = 0x00000080,
        ERROR_BRAKE_DEADTIME_VIOLATION = 0x00000100,
        ERROR_UNEXPECTED_TIMER_CALLBACK = 0x00000200,
        ERROR_CURRENT_SENSE_SATURATION = 0x00000400,
        ERROR_CURRENT_LIMIT_VIOLATION = 0x00001000,
        ERROR_BRAKE_DUTY_CYCLE_NAN = 0x00002000,
        ERROR_DC_BUS_OVER_REGEN_CURRENT = 0x00004000,
        ERROR_DC_BUS_OVER_CURRENT = 0x00008000,
        ERROR_MANUALLY_STOP = 0x00010000
    }

};
